package aom.ju.ss.view.main

import android.Manifest.permission.ACCESS_COARSE_LOCATION
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.os.Message
import android.widget.ImageView
import android.widget.TextView
import androidx.core.app.ActivityCompat.requestPermissions
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import aom.ju.ss.R
import aom.ju.ss.databinding.MainBinding
import aom.ju.ss.mode.LpKey
import aom.ju.ss.utils.*
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.community.CommunityHome
import aom.ju.ss.view.cut.CutHomeFragment
import aom.ju.ss.view.dicker.DickerFragment
import aom.ju.ss.view.shua.HomeShuaFragment
import aom.ju.ss.view.user.UserFragment
import com.google.gson.Gson

class MainActivity : BaseActivity() {

    private lateinit var binding: MainBinding

    val fragments = arrayListOf<Fragment>()

    val title = arrayListOf("刷店", "砍价", "易货", "社群", "我的")

    companion object {
        var mCurrentPosition = 0
    }

    val icon = arrayListOf(
        R.drawable.ic_bg_shaudian,
        R.drawable.ic_bg_kanjia,
        R.drawable.ic_bg_yihuo,
        R.drawable.ic_bg_sheuqn,
        R.drawable.ic_bg_wode
    )

    override fun init() {
        binding = viewBinding as MainBinding



        fragments.add(HomeShuaFragment.getMys())
        fragments.add(CutHomeFragment.getMys())
        fragments.add(DickerFragment.getMys())
        fragments.add(CommunityHome.getMys())
        fragments.add(UserFragment.getMys())

        binding.viewPager.adapter = object : FragmentPagerAdapter(supportFragmentManager, 1) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(position: Int): Fragment {
                return fragments[position]
            }

        }
        binding.tab.setupWithViewPager(binding.viewPager)

        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                mCurrentPosition = position
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
        initTab()


    }

    override fun callBack(event: Message) {
        super.callBack(event)
        if (event.arg1 == LpKey.TO_LOCATE) {//去定位
            when {
                SpUtils.getString("lat").isEmpty() -> {
                    requestPermissions(
                        this,
                        arrayOf(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION),
                        1
                    )
                }
                (System.currentTimeMillis()) >= SpUtils.getLong("loc_time") + 86400000 -> {
                    requestPermissions(
                        this,
                        arrayOf(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION),
                        1
                    )

                }
                else -> {
                    SendMsg.sendEvent(LpKey.LOCATE_OK)
                }
            }

        }
    }

    private fun initTab() {
        for ((i, t) in title.withIndex()) {
            val tab = binding.tab.getTabAt(i)
            tab?.setCustomView(R.layout.tab)
            val img = tab?.customView!!.findViewById<ImageView>(R.id.imageView)
            img.setImageResource(icon[i])
            val tv = tab.customView!!.findViewById<TextView>(R.id.title)
            tv.text = t

        }

    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        LogUtils.e(requestCode.toString() + "grantResults" + Gson().toJson(grantResults))
        if (requestCode == 1 && grantResults.size == 2 && grantResults[0] == 0 && grantResults[1] == 0) {
            getLocationByGaode(this) {
                LogUtils.e("定位成功!!!!!!!!")
                SpUtils.putLong("loc_time", System.currentTimeMillis())
                SpUtils.putString("city", it.city)
                SpUtils.putString("cityCode", it.cityCode)
                SpUtils.putString("province", it.province)
                SpUtils.putString("area", it.district)
                SpUtils.putString("street", it.street)
                SpUtils.putString("lat", it.latitude.toString() + "")
                SpUtils.putString("lng", it.longitude.toString() + "")
                SendMsg.sendEvent(LpKey.LOCATE_OK)
            }
        } else if (requestCode == 1) {

            LogUtils.e("定位失败!!!!!!!!")
            SendMsg.sendEvent(LpKey.LOCATE_ERR)
            "定位失败或没有定位权限,将默认为长沙".show
        }

    }

    override val lay: Int = R.layout.main
}